package com.kunyi.web.service.file.core.transform;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.kunyi.platform.auth.ThreadLocalAuth;
import com.kunyi.platform.tool.util.KyAssertUtil;
import com.kunyi.web.service.file.entity.domain.Upload;
import com.kunyi.web.serviceapi.file.entity.dto.FileInfoDto;

/**
 * @ClassName FileTransform
 * @Description 文件路径生成规则类
 * @Author zhenguo.zhuang
 * @Date 2024/3/13 9:53
 * @Version 1.0
 */
public class FileTransform {
    public static final String YEAR_FORMAT = "yyyy";
    public static final String MONTH_FORMAT = "MM";

    /**
     * @author: zhenguo.zhuang
     * @Description 生成文件存储路径
     * @Date 15:09 2024/3/13
     * @Param :directory 初始化目录
     * @return String
     **/
    public static String generateFilePath(String directory){
        // 文件夹路径 年/月
        DateTime dateTime = new DateTime();
        String year = DateUtil.format(dateTime, YEAR_FORMAT);
        String month = DateUtil.format(dateTime, MONTH_FORMAT);
        if(!directory.endsWith("/")){
            directory =directory+"/";
        }
        String folderPath = directory + year + "/" + month+"/";
        return   folderPath;
    }

    /**
     * @author: zhenguo.zhuang
     * @Description 生成文件存储路径，根据分隔符
     * @Date 15:09 2024/3/13
     * @Param :osSeparator  分隔符号  rootPath:基础目录
     * @return String
     **/
    public static String generateFilePath(String rootPath,String osSeparator){
        // 文件夹路径 年/月
        DateTime dateTime = new DateTime();
        String year = DateUtil.format(dateTime, YEAR_FORMAT);
        String month = DateUtil.format(dateTime, MONTH_FORMAT);
        String folderPath = rootPath + year + osSeparator + month+osSeparator;
        return   folderPath;
    }


    /**
     * @author: zhenguo.zhuang
     * @Description 生成新的文件名称
     * @Date 13:41 2024/3/13
     * @Param : oldName 原来的文件名称
     * @return String 生成新的文件名称
     **/
    public static String generateFileName(String oldName){
        DateTime dateTime = new DateTime();
        String year = DateUtil.format(dateTime, YEAR_FORMAT);
        String month = DateUtil.format(dateTime, MONTH_FORMAT);
        String newName = ThreadLocalAuth.getTenantId() + "_" + year + month + IdUtil.getSnowflake().nextIdStr() + oldName.substring(oldName.lastIndexOf("."));
        return newName;
    }
    /**
     * @author: zhenguo.zhuang
     * @Description 生成文件访问路劲
     * @Date 13:53 2024/3/13
     * @Param fileName:
     * @return String
     **/
    public static String generateFileUrl(String fileName,String url) {
        return url+fileName;
    }
    /**
     * @author: zhenguo.zhuang
     * @Description 获取文件路径
     * @Date 15:09 2024/3/13
     * @Param fileName:文件名称
     * @Param directory:初始化目录
     * @return String
     **/
    public static String getFilePath(String directory ,String fileName) {
        String[] fileNames = fileName.split("_");
        KyAssertUtil.equals(fileNames.length,2,"文件名不合法");
        String folderName = fileNames[1].substring(0, 6);
        DateTime dateTime = DateUtil.parse(folderName, YEAR_FORMAT + MONTH_FORMAT);
        String year = DateUtil.format(dateTime, YEAR_FORMAT);
        String month = DateUtil.format(dateTime, MONTH_FORMAT);

        if(!directory.endsWith("/")){
            directory =directory+"/";
        }
        String path = directory + year + "/" + month + "/" + fileName;
        return path;
    }

    /**
     * @author: zhenguo.zhuang
     * @Description 获取文件路径，根据分隔符
     * @Date 15:09 2024/3/13
     * @Param fileName:
     * @return String
     **/
    public static String getFilePath(String fileName, String rootPath,String osSeparator) {
        String[] fileNames = fileName.split("_");
        KyAssertUtil.equals(fileNames.length,2,"文件名不合法");
        String folderName = fileNames[1].substring(0, 6);
        DateTime dateTime = DateUtil.parse(folderName, YEAR_FORMAT + MONTH_FORMAT);
        String year = DateUtil.format(dateTime, YEAR_FORMAT);
        String month = DateUtil.format(dateTime, MONTH_FORMAT);
        String path = rootPath + year + osSeparator + month + osSeparator + fileName;
        return path;
    }


    public static FileInfoDto upload2FileInfo(Upload upload) {
        return upload == null ? null : new FileInfoDto().setSourceName(upload.getSourceName());
    }
}
